package com.crossriver.crgis.cofo.util;

import java.util.List;

import javax.portlet.ActionRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.crossriver.crgis.database.model.Application_C_of_O;
import com.crossriver.crgis.database.model.Company_Contact_Person;
import com.crossriver.crgis.database.model.Contact_Information;
import com.crossriver.crgis.database.model.History_Of_Information;
import com.crossriver.crgis.database.model.Individual_Information;
import com.crossriver.crgis.database.model.Organisation_Information;
import com.crossriver.crgis.database.model.Property_Information;
import com.crossriver.crgis.database.model.Supporting_Document;
import com.crossriver.crgis.database.service.Application_C_of_OLocalServiceUtil;
import com.crossriver.crgis.database.service.Company_Contact_PersonLocalServiceUtil;
import com.crossriver.crgis.database.service.Contact_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.History_Of_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Individual_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Organisation_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Property_InformationLocalServiceUtil;
import com.crossriver.crgis.database.service.Supporting_DocumentLocalServiceUtil;
import com.crossriver.crgis.portlet.PrintCofO;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;

public class CofORetrieveUtil {
	
	// logger
	private static Log log = LogFactory.getLog(CofORetrieveUtil.class);
 

	public static boolean extractSupportDocForDisplay(ActionRequest actionRequest,
			Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractSupportDocForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application

		List<Supporting_Document> suppDoc = null;

		try {
			suppDoc = Supporting_DocumentLocalServiceUtil
					.getSupporting_DocumentByApplId(appCofOTemp
							.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.SUPP_DOC_INFO_SESSION_ATTR,
					suppDoc);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived Supporting_Document "
						+ "and prepared for page rendering >>>  " + suppDoc);
			}

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Supporting_Document "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;

			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Supporting_Document "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();
		}

		return retrieveInfo;

	}

	public static boolean extractPropertyInfoForDisplay(ActionRequest actionRequest,
			Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractPropertyInfoForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application
		Property_Information propInfo = null;

		try {
			propInfo = Property_InformationLocalServiceUtil
					.getProperty_Information(appCofOTemp.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.PROPERTY_INFO_SESSION_ATTR,
					propInfo);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived Property_Information "
						+ "and prepared for page rendering >>>  " + propInfo);
			}
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Property_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Property_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Property_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;

			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Property_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();
		}

		return retrieveInfo;

	}

	public static boolean extractApplicationHistForDisplay(
			ActionRequest actionRequest, Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractApplicationHistForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application
		List<History_Of_Information> historyInfo = null;

		try {
			historyInfo = History_Of_InformationLocalServiceUtil
					.getHistory_Of_InformationByApplId(appCofOTemp
							.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.HISTORY_INFO_SESSION_ATTR,
					historyInfo);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived History_Of_Information "
						+ "and prepared for page rendering >>>  " + historyInfo);
			}
		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving History_Of_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;

			if (log.isInfoEnabled()) {
				log.info("Problem retreiving History_Of_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();
		}

		return retrieveInfo;
	}

	public static boolean extractContactInfoForDisplay(ActionRequest actionRequest,
			Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractContactInfoForDisplay");
		}

		boolean retrieveInfo = true;
		// retrieve application
		Contact_Information indContInfo = null;

		try {
			indContInfo = Contact_InformationLocalServiceUtil
					.getContact_Information(appCofOTemp.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_CONTACT_SESSION_ATTR,
					indContInfo);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived Contact_Information "
						+ "and prepared for page rendering >>>  " + indContInfo);
			}
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Contact_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Contact_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Contact_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;

			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Contact_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();
		}

		return retrieveInfo;
	}

	public static boolean extractApplicationInfoForDisplay(
			ActionRequest actionRequest, Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractApplicationInfoForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application
		Application_C_of_O appl = null;

		try {
			appl = Application_C_of_OLocalServiceUtil
					.getApplication_C_of_O(appCofOTemp.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR, appl);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived Application information "
						+ "and prepared for page rendering >>>  " + appl);
			}

		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Application information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Application information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Application information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;

			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Application information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();
		}

		return retrieveInfo;
	}

	public static boolean extractPersonalInfoForDisplay(ActionRequest actionRequest,
			Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractPersonalInfoForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application
		Individual_Information indInfo = null;

		try {
			indInfo = Individual_InformationLocalServiceUtil
					.getIndividual_Information(appCofOTemp.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.IND_INFO_SESSION_ATTR, indInfo);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived Individual_Information  "
						+ "and prepared for page rendering >>>  "
						+ indInfo.getApplicationId());
			}
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		}

		return retrieveInfo;
	}

	public static boolean extractOrganisationInfoForDisplay(
			ActionRequest actionRequest, Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractOrganisationInfoForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application
		Organisation_Information orgInfo = null;

		try {
			orgInfo = Organisation_InformationLocalServiceUtil
					.getOrganisation_Information(appCofOTemp.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.ORGANISATION_INFO_SESSION_ATTR,
					orgInfo);
			if (log.isInfoEnabled()) {
				log.info("Successfuly retreived Organisation Information  "
						+ "and prepared for page rendering >>>  "
						+ orgInfo.getApplicationId());
			}
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Individual_Information "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		}

		return retrieveInfo;
	}

	public static boolean extractCompanyContactInfoForDisplay(
			ActionRequest actionRequest, Application_C_of_O appCofOTemp) {
		if (log.isInfoEnabled()) {
			log.info("In method extractCompanyContactInfoForDisplay");
		}
		boolean retrieveInfo = true;
		// retrieve application
		Company_Contact_Person coyContactPerson = null;

		try {
			coyContactPerson = Company_Contact_PersonLocalServiceUtil
					.getCompany_Contact_Person(appCofOTemp.getApplicationId());
			actionRequest.setAttribute(
					ApplicationCofOConstants.COY_CONTACT_SESSION_ATTR,
					coyContactPerson);
			if (log.isInfoEnabled()) {
				log.info("Successfuly company contact   "
						+ "and prepared for page rendering >>>  "
						+ coyContactPerson.getApplicationId());
			}
		} catch (PortalException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Company Contact "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Company Contact "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		} catch (SystemException e) {
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Compnay Contact "
						+ "and preparing for page rendering >>>  "
						+ appCofOTemp.getApplicationId());
			}
			retrieveInfo = false;
			if (log.isInfoEnabled()) {
				log.info("Problem retreiving Compnay Contact "
						+ "and preparing for page rendering >>>  "
						+ e.getMessage());
			}
			//e.printStackTrace();

		}

		return retrieveInfo;
	}


}
